#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <stdexcept>

template<typename T>
class MyVector{

private:

    // 栈中存储
    T *elements;     // 指向动态数组的指针
    size_t capacity; // 数组的容量
    size_t size;     // 数组中元素的个数

public:
    // 构造函数
    MyVector(): elements(nullptr), capacity(0), size(0){}
    // 析构函数
    ~MyVector(){
        delete[] elements;
    }

    //拷贝构造函数

    MyVector(const MyVector &another):capacity(another.capacity), size(another.size)
    {
        elements = new T[capacity];
        std::copy(another.elements, another.elements + size, elements);
    }


    //拷贝赋值操作符
    MyVector &operator= (const MyVector &another)
    {
        if (this != &another)
        {
            delete[] elements;
            capacity = another.capacity;
            size = another.size;
            elements = new T[capacity];
            std::copy(another.elements, another.elements + size, elements);
        }
        return *this;
    }

    //添加元素到末尾
    void push_back(const T &value){
        if(size == capacity){

            // 数组满，则扩充容量
            reserve(capacity == 0 ? 1 : capacity * 2);

        }
        elements[size++] = value;
    }

    //获取数组元素个数
    int getSize() const
    {
        return size;
    }

    //获取数组容量
    size_t getCapacity() const // 常成员函数，不会修改任何成员函数
    {
        return capacity;
    }

    // 访问数组中的元素

    T &operator[] (size_t index) 
    {
        // 检查索引
        if(index >= size){
            throw std::out_of_range("Index out of range");
        }

        return elements[index];
    }

    // 访问数组元素元素 const 版

    const T &operator[] (size_t index) const
    {
        // 检查索引
        if(index >= size){
            throw std::out_of_range("Index out of range");
        }

        return elements[index];
    }

    // 指定位置插入元素
    void insert(size_t index, const T &value)
    {
        if(index > size){
            throw std::out_of_range("Index out of range");
        }

        if(size == capacity){
            reserve(capacity == 0 ? 1 : capacity * 2);
        }

        for(size_t i = index;i < size;i++){
            elements[i + 1] = elements[i];
        }

        elements[index] = value;    
        ++size;
    }

    // 删除末尾元素
    void pop_back()
    {
        if(size > 0){
            --size;
        }
    }

    // 清空数组
    void clear()
    {
        size = 0;
    }

    // 数组开始的位置
    T *begin()
    {
        return elements;
    }

    T *end()
    {
        return elements + size;
        
    }

    const T *begin() const
    {
        return elements;
    }

    const T *end() const
    {
        return elements + size;
    }

    void printElements() const
    {
        for(int i = 0;i < size;++i){
            std::cout << elements[i] <<" ";
        }
        std::cout << std::endl;
    }

private:
    
    // 扩容
    void reserve(size_t new_capacity){
        if(new_capacity > capacity){
            T *new_elements = new T[new_capacity];
            std::copy(new_elements, new_elements + size, elements);
            delete[] elements;
            elements = new_elements;
            capacity = new_capacity;
        }
    }


};